{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e8b27860",
   "metadata": {},
   "source": [
    "# Which\n",
    "\n",
    "分类任务，支持2种模式\n",
    "1. COCO数据集格式，具体格式说明：https://zhuanlan.zhihu.com/p/29393415\n",
    "3. Image-Mask数据集格式，Image一个文件夹，Mask一个文件夹，此模式仍在测试中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d95d52c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 获得视频教程\n",
    "from onekey_algo.custom.Manager import onekey_show\n",
    "onekey_show('Which概览')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c8bdaeb",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 获得视频教程\n",
    "from onekey_algo.custom.Manager import onekey_show\n",
    "onekey_show('labelme-which2d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76f2efb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 获得视频教程\n",
    "from onekey_algo.custom.Manager import onekey_show\n",
    "onekey_show('Which概览|数据转化')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "222e070b",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 获得视频教程\n",
    "from onekey_algo.custom.Manager import onekey_show\n",
    "onekey_show('Which概览|模型训练')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e74379f",
   "metadata": {},
   "source": [
    "### 支持的模型名称\n",
    "\n",
    "模型名称替换代码中的 `model_name`变量的值。\n",
    "\n",
    "| **模型系列** | **模型名称**                                                 |\n",
    "| ------------ | ------------------------------------------------------------ |\n",
    "| FCN      | fcn_resnet50, fcn_resnet101                                                      |\n",
    "| Deeplab          | deeplabv3_resnet50, deeplabv3_resnet101, deeplabv3_mobilenet_v3_large |\n",
    "| lraspp       | lraspp_mobilenet_v3_large |\n",
    "| Unet     | UNet           |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e23e47b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from onekey_algo import get_param_in_cwd\n",
    "from onekey_algo.segmentation.run_segmentation import main as seg_main\n",
    "\n",
    "# 设置参数\n",
    "class params:\n",
    "    dataset = r'xxx_general_image_mask'\n",
    "    data_path = get_param_in_cwd('data_root')\n",
    "    model = get_param_in_cwd('model_name', 'fcn_resnet50')\n",
    "    lr = 0.001\n",
    "    workers = 4\n",
    "    batch_size = 4\n",
    "    val_batch_size = 1\n",
    "    print_freq = 1\n",
    "    epochs = 1\n",
    "    optimizer = 'sgd'\n",
    "    momentum = 0.9\n",
    "    weight_decay = 1e-4\n",
    "    downsample_ratio = 1\n",
    "    save_dir = get_param_in_cwd('save_dir', '.')\n",
    "    resume = r''\n",
    "    dist_url = 'env://'\n",
    "    world_size = 1\n",
    "    pretrained = True\n",
    "    aux_loss = False\n",
    "    test_only = False\n",
    "    save_per_epoch = False\n",
    "    base_size = 480\n",
    "    crop_size = 480\n",
    "    attr = {}\n",
    "\n",
    "    def __setattr__(self, key, value):\n",
    "        self.attr[key] = value\n",
    "\n",
    "# 训练模型\n",
    "seg_main(params)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "188d8ee9",
   "metadata": {},
   "source": [
    "### 批量预测\n",
    "\n",
    "批量进行数据预测，输出的结果左侧为原始数据，右侧为识别结果。\n",
    "\n",
    "* model_root：模型保存的路径，需要具体到viz目录，\n",
    "   > 例如`model_root = r'path2your_model_root\\20220601\\deeplabv3_resnet101\\viz'`\n",
    "   \n",
    "* test_samples：需要测试的样本集合\n",
    "   > 例如我们测试所有的val数据集的结果，`test_samples = glob.glob(os.path.join(save_dir, 'val', 'JPEGImages', '*.jpg'))`\n",
    "   \n",
    "* save_dir：测试结果输出目录，自己可以按需指定。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70e2172d",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 获得视频教程\n",
    "from onekey_algo.custom.Manager import onekey_show\n",
    "onekey_show('Which概览|批量预测')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03b274ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "import glob\n",
    "import os\n",
    "from onekey_algo.segmentation.eval_segmentation import init, inference\n",
    "\n",
    "model_root = os.path.join(get_param_in_cwd('save_dir', '.'), get_param_in_cwd('model_name'), 'viz')\n",
    "test_samples = glob.glob(os.path.join(get_param_in_cwd('data_root'), '*', 'images', r'*[jpg|png|bmp]'))\n",
    "save_dir = os.path.join(os.path.join(get_param_in_cwd('save_dir', '.'), get_param_in_cwd('model_name'), 'test_results'))\n",
    "\n",
    "model, class_names, device = init(model_root)\n",
    "results = inference(test_samples, model, device=device, class_names=class_names, save_dir=save_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9258088",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
